package com.heatingcharge.service.unit.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.heatingcharge.common.exception.BaseException;
import com.heatingcharge.common.util.BeanValidator;
import com.heatingcharge.common.util.ResponseCodeEnum;
import com.heatingcharge.dto.ResBuildingDTO;
import com.heatingcharge.dto.ResZoneDTO;
import com.heatingcharge.dto.ZoneClearOffDTO;
import com.heatingcharge.entity.sys.SysUser;
import com.heatingcharge.entity.unit.ResZone;
import com.heatingcharge.mapper.unit.ResZoneMapper;
import com.heatingcharge.param.ResZoneParam;
import com.heatingcharge.param.SearchParam;
import com.heatingcharge.param.ZoneTreeParam;
import com.heatingcharge.service.charge.IClearOffService;
import com.heatingcharge.service.unit.IResBuildingService;
import com.heatingcharge.service.unit.IResZoneService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heatingcharge.vo.TreeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 小区  服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-07-12
 */
@Service
public class ResZoneServiceImpl extends ServiceImpl<ResZoneMapper, ResZone> implements IResZoneService {

    @Autowired
    IResBuildingService resBuildingService;
    @Autowired
    IClearOffService clearOffService;

    @Override
    public IPage<ResZone> selectZonePage(Page page, ResZoneDTO resZoneDTO) {
        return baseMapper.selectZone(page,resZoneDTO);
    }

    @Override
    public List<ResZone> selectZone(ResZoneDTO resZoneDTO) {
        List<ResZone> zones = baseMapper.selectZone();
        return zones;
    }
    @Override
    public List<TreeVo> selectZoneTree(ZoneTreeParam zoneTreeParam) {
        List<ResZone> zones = baseMapper.selectZone();
        List<TreeVo> treeVos= Lists.newArrayList();
        TreeVo treeVo;
        ResBuildingDTO resBuildingDTO;
        if (zones.size()>0){
            for (int i = 0; i < zones.size(); i++) {
                resBuildingDTO=ResBuildingDTO.builder()
                        .zoneId(zones.get(i).getId())
                        .showUnit(true)
                        .showStatistics(true)
                        .heatYearId(zoneTreeParam.getHeatYearId())
                        .build();
                if (zoneTreeParam.getShowUnit().equals(false)){
                    resBuildingDTO.setShowUnit(false);
                }
                if (zoneTreeParam.getShowStatistics().equals(true)){
                    resBuildingDTO.setShowStatistics(true);
                }
                treeVo=TreeVo.builder()
                        .value(zones.get(i).getId())
                        .id("zoneId="+zones.get(i).getId())
                        .label(zones.get(i).getName())
                        .type("zone")
                        .children(resBuildingService.selectBuildingTree(resBuildingDTO))
                        .build();
                if (zoneTreeParam.getShowStatistics().equals(true)){
                    SearchParam searchParam=new SearchParam(zones.get(i).getId(),0,zoneTreeParam.getHeatYearId());
                    ZoneClearOffDTO allZonesStatistics = clearOffService.getAllZonesStatistics(searchParam);
                    treeVo.setZoneClearOffDTO(allZonesStatistics);
                }
                treeVos.add(treeVo);
            }
        }
        return treeVos;
    }

    @Override
    public boolean save(ResZoneParam resZoneParam) {
        //校验字段合法性
        BeanValidator.check(resZoneParam);
        //检验小区是否已经存在
        if (checkExist(resZoneParam.getName(),null)){
            //小区已经存在
            throw new BaseException(ResponseCodeEnum.RESZONE_EXIST.getMessage(),ResponseCodeEnum.RESZONE_EXIST.getCode());
        }
        ResZone resZone=ResZone.builder()
                .name(resZoneParam.getName())
                .addr(resZoneParam.getAddr())
                .build();
        return baseMapper.insert(resZone)>0;
    }
    /**
     * 系统中是否存在此小区
     * @param name
     * @param id
     * @return
     */
    private boolean checkExist(String name, Integer id) {
        QueryWrapper<ResZone> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        if (id!=null){
            queryWrapper.ne("id",id);
        }
        return this.list(queryWrapper).size()>0;
    }
    @Override
    public boolean update(ResZoneParam resZoneParam) {
        //校验字段合法性
        BeanValidator.check(resZoneParam);
        //检验换热站是否已经存在
        if (checkExist(resZoneParam.getName(), resZoneParam.getId())){
            //小区已经存在
            throw new BaseException(ResponseCodeEnum.RESZONE_EXIST.getMessage(),ResponseCodeEnum.RESZONE_EXIST.getCode());
        }
        ResZone before=this.getById(resZoneParam.getId());
        if (ObjectUtil.isNull(before)){
            throw new BaseException(ResponseCodeEnum.RESZONE_NON_EXIST.getMessage(),ResponseCodeEnum.RESZONE_NON_EXIST.getCode());
        }
        ResZone after=new ResZone();
        BeanUtils.copyProperties(resZoneParam, after);

        return baseMapper.updateById(after)>0;
    }
}
